package com.example.sneakerappforme1105.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.sneakerappforme1105.AddressActivity;
import com.example.sneakerappforme1105.DataProvider;
import com.example.sneakerappforme1105.MainActivity;
import com.example.sneakerappforme1105.R;
import com.example.sneakerappforme1105.RootActivity;
import com.example.sneakerappforme1105.ShowOrderActivity;
import com.example.sneakerappforme1105.bean.Goods;

import java.util.HashMap;

public class MyFragment extends Fragment {
    private String TAG = "HomeFragment";
    private View mView;
    private AppCompatActivity mActivity;
    private LinearLayout ll_address; // 点击展示收货地址
    private Long mUserId;
    private Long mCartId;
    private HashMap<Long, Goods> mGoodsMap = new HashMap<>(); // long 是info.goodsId作为外键
    private RadioGroup radioGroup;
    private RadioButton rb_pay; // 1
    private RadioButton rb_fahuo;   // 2
    private RadioButton rb_shouhuo; // 3
    public MyFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "MyFragment 启动");
        mActivity = (AppCompatActivity) getActivity();
        mView = inflater.inflate(R.layout.fragment_my, container, false);

        ll_address = mView.findViewById(R.id.ll_address);
        ll_address.setOnClickListener(view -> {
            // 点击跳转到地址管理页面
            Intent intent = new Intent(mActivity, AddressActivity.class);
            intent.putExtra("userId", mUserId); // 根据用户id来进行查询
            startActivity(intent);
        });

        DataProvider dataProvider = (DataProvider) getActivity();   // 用来得到 所挂载活动的成员变量

        if(dataProvider!=null){
            Log.d(TAG,"dataProvider不为空");
            mUserId = dataProvider.getUserId();
            mCartId = dataProvider.getCartId();
            mGoodsMap = dataProvider.getGoodsMap(); // 传递给下一个活动
        }
        Log.d(TAG,"用户id"+mUserId);
        Log.d(TAG,"购物车id"+mCartId);

        rb_pay = mView.findViewById(R.id.rb_pay);
        // 按钮进行跳转, 跳转回my
        rb_pay.setOnClickListener(view -> {
            Intent intent = new Intent(mActivity, ShowOrderActivity.class);
            intent.putExtra("userId", mUserId); // 根据用户id来进行查询查询订单
            intent.putExtra("cartId", mCartId);
            intent.putExtra("idx",1);
            startActivity(intent);
        });

        rb_fahuo = mView.findViewById(R.id.rb_fahuo);
        // 按钮进行跳转
        rb_fahuo.setOnClickListener(view -> {
            Intent intent = new Intent(mActivity, ShowOrderActivity.class);
            intent.putExtra("idx",2);
            intent.putExtra("cartId", mCartId);
            intent.putExtra("userId", mUserId); // 根据用户id来进行查询查询订单
            startActivity(intent);
        });

        rb_shouhuo = mView.findViewById(R.id.rb_shouhuo);
        // 按钮进行跳转
        rb_shouhuo.setOnClickListener(view -> {
            Intent intent = new Intent(mActivity, ShowOrderActivity.class);
            intent.putExtra("idx",3);
            intent.putExtra("cartId", mCartId);
            intent.putExtra("userId", mUserId); // 根据用户id来进行查询查询订单
            startActivity(intent);
        });

        mView.findViewById(R.id.bt_logout).setOnClickListener(view -> {
            Intent intent = new Intent(requireContext(), MainActivity.class);
            startActivity(intent);
        });




        return mView;
    }
}
